package com.zhanghaijun.createCode.mysql.factory;



import com.zhanghaijun.createCode.util.PrintUtil;
import com.zhanghaijun.createCode.util.PropsUtils;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by zhanghaijun on 15/6/15.
 */
public class MysqlConnection {


    public static Connection getConnection(){
        Connection conn = null;
        try{
            String driver = "com.mysql.jdbc.Driver";
            Class.forName(driver);
            conn = DriverManager.getConnection(PropsUtils.getProp("misc.url"),PropsUtils.getProp("misc.user"), PropsUtils.getProp("misc.passowrd"));
        }catch (Exception e){
            e.printStackTrace();
        }
        return conn;
    }

    public static Map<String,String> getRes(){
        Map<String,String> map = new HashMap<String,String>();
        Connection conn=null;
        Statement statement=null;
        try{
            conn = getConnection();
            statement = conn.createStatement();
            String sql = "select * from T_Access_User";
            ResultSet rs = statement.executeQuery(sql);

            while(rs.next()){
                String DisplayName = rs.getString("DisplayName");
                String LoginName = rs.getString("LoginName");
                map.put(DisplayName,LoginName);
            }

        }catch (Exception e){
            e.printStackTrace();
        }finally {
            try{conn.close();}catch (Exception e){e.printStackTrace();}
            try{statement.close();}catch (Exception e){e.printStackTrace();}
        }
        return map;

    }


    public static void main(String[] args)throws Exception{
        Map<String,String> map = getRes();
    }



}
